home *** CD-ROM | disk | FTP | other *** search
/ Alles Voor Internet / Tout Pour Internet / alles voor internet.iso / MacInternet™ / Telnet / NCSA / tn3270 2.4d7 source / NCSA⁄BYU TCP⁄IP / croft.c < prev    next >
Text File  |  1991-10-02  |  8KB  |  387 lines

  1. #ifndef lint
  2. static char *SCCSid = "%W%    (NCSA)    %G%";
  3. #endif
  4. /*
  5. *    croft.c
  6. *     by Gaige B. Paulsen
  7. ****************************************************************************
  8. *                                                                          *
  9. *      Uses    :                                                               *
  10. *      TCP/IP kernel for NCSA Telnet                                       *
  11. *      by Tim Krauskopf                                                    *
  12. *       with Macintosh code by Gaige B. Paulsen                                 *
  13. *                                                                          *
  14. *      National Center for Supercomputing Applications                     *
  15. *      152 Computing Applications Building                                 *
  16. *      605 E. Springfield Ave.                                             *
  17. *      Champaign, IL  61820                                                *
  18. *                                                                          *
  19. *                                                                          *
  20. ****************************************************************************
  21. *
  22. *    KIP/Croft gateway handling code.    
  23. *
  24. *    Called by:
  25. *        maclook.c
  26. *        mactools.c
  27. *        macutil.c
  28. */
  29.  
  30. #include <stdio.h>
  31. #include <string.h>
  32.  
  33. #include <AppleTalk.h>
  34. #include <Dialogs.h>
  35. #include <Memory.h>
  36. #include <Strings.h>
  37.  
  38. #include "configrec.h"
  39. #include "kip.h"
  40. #include "maclook.h"
  41. #include "mpw.h"
  42.  
  43. extern void OtherError();    /* BYU */
  44.  
  45. #ifdef OLDM
  46. struct NBPentadd {
  47.     AddrBlock addr;
  48.     EntityName entity;
  49.     };
  50. typedef struct NBPentadd NBPentadd;
  51.  
  52. NBPRecHdl
  53.     myRecH;            /* Handle used for AB records */
  54. #endif OLDM
  55.  
  56. char
  57.     KIPzone[33]="\p*",    /* Kip zone in P-string */
  58.     *KIPnameptr=0L;    /* Pointer to current name block (NBP) */
  59.  
  60. void KIPunregister
  61.   (
  62.     void
  63.   )
  64. {
  65.     MPPParamBlock Mpb;
  66.     int i;
  67.     char temp[50];
  68.     
  69.     Mpb.NBPentityPtr = &KIPnameptr[9];
  70.     i =PRemoveName(&Mpb, FALSE);
  71.     sprintf(temp, "RN=%d",i);
  72.     putln(temp);
  73. }
  74.  
  75. #ifdef DEBUGG
  76.  
  77. /*
  78.  *    NBPdisplay- display an NBP packet (eap) in a reasonable format 
  79.  *
  80.  */
  81.  
  82. NBPdisplay( eap)
  83. NTElement *eap;
  84. {
  85.     unsigned char *cp;
  86.     int len1,len2;
  87.  
  88.     cp=(char *)eap+3;
  89.     len1= cp[1];
  90.     len2= cp[ len1+2];
  91.  
  92.     printf("Name: %*s\nType: %*s\nZone: *\nAtalk Address: %d.%d.%d\n",
  93.         len1,&cp[2], len2, &cp[ len1+3],
  94.         eap->nteAddress.aNet, eap->nteAddress.aNode, eap->nteAddress.aSocket);
  95. }
  96. #endif DEBUGG
  97.  
  98. /*
  99.  *    NBPfind - check for <object,typename,zone> wikth the resultant address
  100.  *            in buffer which is bufsize bytes.  Expect expect responses.
  101.  *
  102.  *    returns <0 if NBP/network error
  103.  *            =0 if not found
  104.  *            >0 if ok
  105.  */
  106.  
  107. int NBPfind( object,typename,zone, buffer,bufsize, expect,rti)
  108. Str32 *object, *typename, *zone;
  109. char *buffer;
  110. int rti,expect,bufsize;
  111. {
  112.     MPPParamBlock Mpb;
  113.     EntityName entity;
  114.     int err;
  115.  
  116.     NBPSetEntity((Ptr) &entity, (Ptr) object, (Ptr) typename, (Ptr) zone);
  117.  
  118.     Mpb.NBPentityPtr = (Ptr) &entity;
  119.     Mpb.NBPretBuffPtr=buffer;
  120.     Mpb.NBPretBuffSize=bufsize;
  121.     Mpb.NBPmaxToGet=expect;
  122.     Mpb.NBPinterval= rti;
  123.     Mpb.NBPcount   = 4;    
  124.     err= PLookupName(&Mpb, FALSE);
  125.     if (err!=0) {
  126.         putln("Error in NBP Lookup.....%d\n");
  127.         return(-1);
  128.         }
  129.     else return(Mpb.NBPnumGotten);
  130. }
  131.  
  132. /*
  133.  *    NBPregister - Register on the network as <object,typename,zone> using
  134.  *            buffer of size to store permanent information.
  135.  *
  136.  *    returns <0 if NBP/network error
  137.  *            >=0 if ok
  138.  */
  139. int NBPregister( object,typename,zone, buffer,size )
  140. Str32 *object, *typename, *zone;
  141. NamesTableEntry *buffer;
  142. int size;
  143. {
  144. #pragma unused(size)
  145.     MPPParamBlock Mpb;
  146.     int err;
  147.     THz curzone;
  148.     
  149.     if (KIPnameptr)
  150.         KIPunregister();
  151.     curzone = GetZone();
  152.     SetZone(SystemZone());
  153.     (Ptr) KIPnameptr = (Ptr) buffer = NewPtr((long)sizeof(NamesTableEntry));
  154.     SetZone(curzone);
  155.  
  156.     NBPSetNTE((Ptr) buffer, (Ptr) object, (Ptr) typename, (Ptr) zone, 72);
  157.  
  158.     Mpb.NBPntQElPtr = (Ptr) buffer;
  159.     Mpb.NBPinterval= 5;
  160.     Mpb.NBPcount   = 5;    
  161.     Mpb.NBPverifyFlag=1;    
  162.  
  163.     err= PRegisterName(&Mpb, FALSE);
  164.     if (err!=0) {
  165.         putln("Error in NBP Register.....%d\n");
  166.         DisposPtr((Ptr) buffer);
  167.         return(-1);
  168.         }
  169.     else return(Mpb.MPPioResult);
  170. }
  171.  
  172. /*
  173.  *    ATPsend - sends a request to the ATP server at addr using the packet in
  174.  *            buffer which is size bytes long puting the return data in the 
  175.  *            retdata BDS.
  176.  *
  177.  *    returns <0 if ATP/network error
  178.  *            >=0 if ok
  179.  */
  180.  
  181. ATPsend( addr, buffer, size, retdata)
  182. AddrBlock *addr;
  183. char *buffer;
  184. int size;
  185. BDSPtr retdata;
  186. {
  187.     ATPParamBlock Apb;
  188.     int err;
  189.  
  190.     Apb.ATPaddrBlock=*addr;
  191.     Apb.ATPreqLength=size;
  192.     Apb.ATPreqPointer=buffer;
  193.     Apb.ATPbdsPointer = (Ptr) retdata;
  194.     Apb.ATPatpFlags=0;
  195.     Apb.ATPtimeOutVal=5;
  196.     Apb.ATPretryCount=4;
  197.     Apb.ATPnumOfBuffs=1;
  198.  
  199.     err=PSendRequest( &Apb, FALSE);
  200.  
  201.     return(err);
  202. }
  203.  
  204. /*
  205.  *    KIParp - Use the croft code to arp for machine with the IP number ipnum,
  206.  *        returning the AT address in addrloc.
  207.  *
  208.  *    returns <0 if NBP/network error
  209.  *            >=0 if ok
  210.  */
  211.  
  212. int KIParp
  213.   (
  214.     unsigned char ipnum[4],
  215.     AddrBlock *addrloc
  216.   )
  217. {
  218.     NTElement retbuff;
  219.     int ret;
  220.     char temps[20];
  221.  
  222.     sprintf(temps," %d.%d.%d.%d", (int)ipnum[0],(int)ipnum[1],(int)ipnum[2],
  223.             (int)ipnum[3]);
  224.  
  225. #ifdef DEBUGG
  226.     puts(temps); puts(" is being looked up\n");
  227. #endif
  228.  
  229.     temps[0]=strlen(temps)-1;
  230.     ret=NBPfind( temps,"\pIPADDRESS",KIPzone,&retbuff,sizeof(retbuff),1,40);
  231.  
  232. #ifdef DEBUGG
  233.     NBPdisplay(&retbuff);
  234. #endif DEBUGG
  235.  
  236.     *addrloc=retbuff.nteAddress;
  237.     return(ret);
  238. }
  239.  
  240. /*
  241.  *    KIPregister - Use the croft code to register our machine on the network as
  242.  *            ipno.
  243.  *
  244.  *    returns <0 if NBP/network error
  245.  *            >=0 if ok
  246.  */
  247.  
  248. int KIPregister
  249.   (
  250.     unsigned char *ipnum
  251.   )
  252. {
  253.     char temps[20];
  254.     int ret;
  255.  
  256.     sprintf(temps," %d.%d.%d.%d", (int)ipnum[0],(int)ipnum[1],(int)ipnum[2],
  257.             (int)ipnum[3]);
  258. #ifdef DEBUGG
  259.     puts(temps); puts(" is being registered \n");
  260. #endif
  261. #ifdef DONTDOTHISRIGHTNOW
  262.     KIPnameptr=NewPtr(110);
  263.     if (KIPnameptr==0L) return(-1);
  264. #endif
  265.     temps[0]=strlen(temps)-1;
  266.     ret=NBPregister( temps, "\pIPADDRESS", KIPzone , KIPnameptr,110);
  267.     return(ret);
  268. }
  269.  
  270. /*
  271.  *    KIPgetdynam - Use the croft code to get a dynamic IP number for us from
  272.  *            the server at servAddr. The IP number is stored in IPaddr.
  273.  *
  274.  *    returns <0 if NBP/network error
  275.  *            >=0 if ok
  276.  */
  277.  
  278. int KIPgetdynam
  279.   (
  280.     AddrBlock *servAddr,
  281.     long *IPaddr
  282.   )
  283. {
  284.     IPGP gateRec,gateSend;
  285.     BDSType retBDS;
  286.     int err;
  287.  
  288.     retBDS[0].buffSize=sizeof(gateRec);
  289.     retBDS[0].buffPtr = (Ptr) &gateRec;
  290.  
  291.     gateSend.opcode=ipgpAssign;
  292.  
  293.     err=ATPsend( servAddr, &gateSend,sizeof(gateSend), retBDS);
  294.  
  295.     if (err!=0) 
  296.         {
  297.         *IPaddr = 0L;
  298. #ifndef MPW
  299.         OtherError("\PUnable to get dynamic IP number","\P ");
  300. #else
  301.         OtherError("Unable to get dynamic IP number"," ");
  302. #endif MPW
  303.  
  304.         return(err);
  305.         }
  306.         
  307.     *IPaddr=gateRec.ipaddress;
  308. #ifdef DEBUGG
  309.     if (gateRec.opcode<0) puts(gateRec.string);
  310. #endif
  311.     return(0);
  312. }
  313.  
  314. /*
  315.  *    KIPgetns - Use the croft code to get the IP number of the Nameserver for us from
  316.  *            the server at servAddr. The IP number is stored in IPaddr.
  317.  *
  318.  *    returns <0 if NBP/network error
  319.  *            >=0 if ok
  320.  */
  321.  
  322. int KIPgetns
  323.   (
  324.     AddrBlock *servAddr,
  325.     long *IPaddr
  326.   )
  327. {
  328.     IPGP gateRec,gateSend;
  329.     BDSType retBDS;
  330.     int err;
  331.  
  332.     retBDS[0].buffSize=sizeof(gateRec);
  333.     retBDS[0].buffPtr = (Ptr) &gateRec;
  334.  
  335.     gateSend.opcode=ipgpServer;
  336.  
  337.     err=ATPsend( servAddr, &gateSend,sizeof(gateSend), retBDS);
  338.  
  339.     if (err!=0) return(err);
  340.     *IPaddr=gateRec.ipname;
  341. #ifdef DEBUGG
  342.     if (gateRec.opcode<0) puts(gateRec.string);
  343. #endif
  344.     return(0);
  345. }
  346.  
  347. /*
  348.  *    KIPfindgate - Use the croft code to find a gateway to use.  Returns the
  349.  *            AT address in addr.
  350.  *
  351.  *    returns <0 if NBP/network error
  352.  *            =0 if there are none.
  353.  *            >0 if ok
  354.  */
  355.  
  356. int KIPfindgate
  357.   (
  358.     AddrBlock *addr
  359.   )
  360. {
  361.     NTElement retbuff;
  362.     int ret;
  363.  
  364.     ret=NBPfind( "\p=","\pIPGATEWAY", KIPzone,&retbuff,sizeof(retbuff),1,3);
  365.  
  366. #ifdef DEBUGG
  367.     NBPdisplay(&retbuff);
  368. #endif DEBUGG
  369.  
  370.     *addr=retbuff.nteAddress;
  371.     return(ret);
  372. }
  373.  
  374. /* KIPsetzone - set up KIPzone */
  375.  
  376. void KIPsetzone
  377.   (
  378.     char *zone
  379.   )
  380. {
  381.     strncpy(KIPzone,zone,32);        /* Copy incoming */
  382.     KIPzone[32]=0;                    /* NUL terminate */
  383.     c2pstr(KIPzone);                /* Convert to pascal string */
  384.     putln("new zone is:");
  385.     putln(zone);
  386. }
  387.